package com.example.administrator.dagger2demo.ui.index;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.example.administrator.dagger2demo.R;
import com.example.administrator.dagger2demo.application.DemoApplication;
import com.example.administrator.dagger2demo.ui.fragment.FragmentI;
import com.example.administrator.dagger2demo.ui.fragment.FragmentIII;
import com.example.administrator.dagger2demo.ui.fragment.FragmentII;
import com.example.administrator.dagger2demo.ui.fragment.FragmentIIII;
import com.example.administrator.dagger2demo.ui.fragment.FragmentIIIII;
import com.example.administrator.dagger2demo.ui.Base2Activity;
import com.example.administrator.dagger2demo.view.NavigationTabBar;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/4/13.
 */
public class IndexActivity extends Base2Activity implements IndexContract.View {

    IndexContract.IndexActionsListener indexActionsListener;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        indexActionsListener = DaggerIndexContractComponent
                .builder()
                .appComponent(DemoApplication.getAppComponent())
                .indexPresenterModule(new IndexPresenterModule(this))
                .build().getLoginPresenter();
        Log.i("TAG", indexActionsListener == null ? "indexActionsListener==null" : "indexActionsListener!=null");
        initUI();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.runFinalization();
        Runtime.getRuntime().gc();
        System.gc();
    }

    class CountFragmentPagerAdapter extends FragmentPagerAdapter {
        public ArrayList<Fragment> fs;

        public CountFragmentPagerAdapter(FragmentManager f, ArrayList<Fragment> fs) {
            super(f);
            this.fs = fs;
        }

        @Override
        public Fragment getItem(int position) {
            return fs.get(position);
        }

        @Override
        public int getCount() {
            return fs.size();
        }
    }

    private void initUI() {
        final ViewPager viewPager = (ViewPager) findViewById(R.id.vp_horizontal_ntb);
        ArrayList<Fragment> fs = new ArrayList<>();
        Bundle args1 = new Bundle();
        args1.putInt("ARG_PAGE", 1);
        Bundle args2 = new Bundle();
        args2.putInt("ARG_PAGE", 2);
        Bundle args3 = new Bundle();
        args3.putInt("ARG_PAGE", 3);
        Bundle args4 = new Bundle();
        args4.putInt("ARG_PAGE", 4);
        Bundle args5 = new Bundle();
        args5.putInt("ARG_PAGE", 5);

        FragmentI fragmentI =  FragmentI.newInstance(args1,indexActionsListener);
        FragmentII fragmentII =   FragmentII.newInstance(args2);
        FragmentIII fragment3 =   FragmentIII.newInstance(args3);
        FragmentIIII fragmentIIII =   FragmentIIII.newInstance(args4);
        FragmentIIIII fragmentIIIII =   FragmentIIIII.newInstance(args5);
        fs.add(fragmentI);
        fs.add(fragmentII);
        fs.add(fragment3);
        fs.add(fragmentIIII);
        fs.add(fragmentIIIII);
        viewPager.setAdapter(new CountFragmentPagerAdapter(getSupportFragmentManager(), fs));


        final String[] colors = getResources().getStringArray(R.array.default_preview);

        final NavigationTabBar navigationTabBar = (NavigationTabBar) findViewById(R.id.ntb_horizontal);
        final ArrayList<NavigationTabBar.Model> models = new ArrayList<>();
        models.add(new NavigationTabBar.Model(
                getResources().getDrawable(R.mipmap.icon_1460189176668), Color.parseColor(colors[0])));
        models.add(new NavigationTabBar.Model(
                getResources().getDrawable(R.mipmap.icon_1460187505919), Color.parseColor(colors[1])));
        models.add(new NavigationTabBar.Model(
                getResources().getDrawable(R.mipmap.icon_1460191278335), Color.parseColor(colors[2])));
        models.add(new NavigationTabBar.Model(
                getResources().getDrawable(R.mipmap.icon_1460188615324), Color.parseColor(colors[3])));
        models.add(new NavigationTabBar.Model(
                getResources().getDrawable(R.mipmap.icon_1460188636401), Color.parseColor(colors[4])));
        navigationTabBar.setModels(models);
        navigationTabBar.setViewPager(viewPager, 0);

        navigationTabBar.setOnTabBarSelectedIndexListener(new NavigationTabBar.OnTabBarSelectedIndexListener() {
            @Override
            public void onStartTabSelected(final NavigationTabBar.Model model, final int index) {

            }

            @Override
            public void onEndTabSelected(final NavigationTabBar.Model model, final int index) {
                model.hideBadge();
            }
        });

        navigationTabBar.post(new Runnable() {
            @Override
            public void run() {
                final View bgNavigationTabBar = findViewById(R.id.bg_ntb_horizontal);
                bgNavigationTabBar.getLayoutParams().height = (int) navigationTabBar.getBarHeight();
                bgNavigationTabBar.requestLayout();
            }
        });

        navigationTabBar.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < navigationTabBar.getModels().size(); i++) {
                    final NavigationTabBar.Model model = navigationTabBar.getModels().get(i);
                    switch (i) {
                        case 0:
                            model.setBadgeTitle("NTB");
                            break;
                        case 1:
                            model.setBadgeTitle("with");
                            break;
                        case 2:
                            model.setBadgeTitle("title");
                            break;
                        case 3:
                            model.setBadgeTitle("badge");

                            break;
                        case 4:
                            model.setBadgeTitle("777");
                            break;
                    }
                    if (i == 3) {
                        navigationTabBar.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                model.showBadge();
                            }
                        }, i * 100);
                    }

                }


            }
        }, 500);

    }

    @Override
    public int getContentView() {
        return R.layout.index;
    }

    @Override
    public boolean isShowLeftIcon() {
        return false;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_launcher;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return R.string.app_name;
    }



    @Override
    public void showProgress() {

    }

    @Override
    public void closeProgress() {

    }

    @Override
    public void startShowView() {

    }

}
